<?php
/**
 * Created by PhpStorm.
 * User: bzing
 * Date: 2017-08-18
 * Time: 10:10
 */
namespace app\admin\controller;
use app\models\Models;

class Count extends Check{
    /**
     * 商家统计
     * @return \think\response\View
     */
    public function countBusiness(){
        $get = input('param.');
        $time = strtotime(date('Y-m-d'));
        $order = new Models('order');
        $where = ['type_id'=>[['eq',Models::ORDER_TYPE_WINE],['eq',Models::ORDER_TYPE_EVENT],'or'],'create_time'=>['gt',$time],'status'=>Models::ORDER_PAY_YES];
        $field = 'bid,sum(pay_amount) amount,count(id) order_num';
        // 获取当日数据
        $arr = $order->_select($where,$field,'','','bid');
        $business = new Models('business');
        $pageSize = 15;
        $listObj = $business->_page(['id'=>['neq',Models::OFFICIAL]],'id,name,amount,order_num','',[],$get['page'],$pageSize);
        $list = $listObj->toArray();
        $i = empty($get['page']) ? 1 : ($get['page'] - 1) * $pageSize + 1;
        foreach($list['data'] as $k=>$v){
            $list['data'][$k]['sort'] = $i++;
            $list['data'][$k]['today_amount'] = 0;
            $list['data'][$k]['today_order'] = 0;
            foreach($arr as $k1=>$v1){
                if($v['id'] == $v1['bid']){
                    $list['data'][$k]['today_amount'] = $v1['amount'];
                    $list['data'][$k]['today_order']  = $v1['order_num'];
                }
            }
        }
        if($get['key'] && $get['sort']){
            $key  = $get['key'];
            $sort = $get['sort'] == 'asc' ? SORT_ASC : SORT_DESC;
            $column = array_column($list['data'],$key);     // 获取列数据
            array_multisort($column,$sort,$list['data']);  // 多维数组的排序
        }
        return view('countBusiness',['list'=>$list['data'],'page'=>$listObj->render()]);
    }

    /**
     * 艺人统计
     * @return \think\response\View
     */
    public function countActor(){
        $get = input('param.');
        $time = strtotime(date('Y-m-d'));
        $follow = new Models('follow');
        // 获取日关注数据
        $where = 'a.follow_object = '.Models::FOLLOW_USER.' and b.type_id = '.Models::USER_TYPE_ACTOR.' and a.status = '.Models::FOLLOW_YES.' and a.create_time > '.$time;
        $arr = $follow->joinMany([['user b','a.follow_id = b.id','left']],$where,'a.follow_id,count(a.fans_id) today_num','','','a.follow_id');
        // 获取所有数据
        $where ='a.type_id = '.Models::USER_TYPE_ACTOR;
        $user = new Models('user');
        $pageSize = 15;
        $listObj = $user->_page($where,'a.id,a.nickname,a.sex,a.fans_num,b.name type_name,c.name business','',[['user_actor_type b','a.actor_id = b.id','left'],['business c','a.up_id = c.id','left']],$get['page'],$pageSize);
        $list = $listObj->toArray();
        $i = empty($get['page']) ? 1 : ($get['page'] - 1) * $pageSize + 1;
        foreach($list['data'] as $k=>$v){
            $list['data'][$k]['sort'] = $i++;
            $list['data'][$k]['today_num'] = 0;
            foreach($arr as $k1=>$v1){
                if($v['id'] == $v1['follow_id']){
                    $list['data'][$k]['today_num'] = $v1['today_num'];
                }
            }
        }
        if($get['key'] && $get['sort']){
            $key  = $get['key'];
            $sort = $get['sort'] == 'asc' ? SORT_ASC : SORT_DESC;
            $column = array_column($list['data'],$key);     // 获取列数据
            array_multisort($column,$sort,$list['data']);  // 多维数组的排序
        }
        return view('countActor',['list'=>$list['data'],'page'=>$listObj->render()]);
    }

    /**
     * 商品统计
     * @return \think\response\View
     */
    public function countGoods(){
        $get = input('param.');
        $time = strtotime(date('Y-m-d'));
        $order = new Models('order');
        // 获取日商品数据
        $where = 'a.status = '.Models::ORDER_PAY_YES.' and a.type_id != '.Models::ORDER_TYPE_RECHARGE.' and a.create_time > '.$time;
        $gid_arr = $order->joinMany([['order_goods b','a.id = b.oid','left'],['goods c','b.gid = c.id','left']],$where,'b.gid,sum(b.qty) today_qty,(c.price*sum(b.qty)) today_amount','','','b.gid');
        // 获取所有商品数据
        $where = 'a.status = '.Models::ORDER_PAY_YES.' and a.type_id != '.Models::ORDER_TYPE_RECHARGE;
        $gid_all = $order->joinMany([['order_goods b','a.id = b.oid','left'],['goods c','b.gid = c.id','left']],$where,'b.gid,sum(b.qty) qty,(c.price*sum(b.qty)) amount','','','b.gid');

        // 获取所有数据
        $goods = new Models('goods');
        $where = '';
        $pageSize = 15;
        $listObj = $goods->_page($where,'a.id,a.name,b.name type_name','',[['goods_type b','a.type_id = b.id','left']],$get['page'],$pageSize);
        $list = $listObj->toArray();
        $i = empty($get['page']) ? 1 : ($get['page'] - 1) * $pageSize + 1;
        foreach($list['data'] as $k=>$v){
            $list['data'][$k]['sort'] = $i++;
            $list['data'][$k]['today_qty'] = 0;
            $list['data'][$k]['today_amount'] = 0;
            $list['data'][$k]['qty'] = 0;
            $list['data'][$k]['amount'] = 0;
            foreach($gid_arr as $k1=>$v1){
                if($v['id'] == $v1['gid']){
                    $list['data'][$k]['today_qty'] = $v1['today_qty'];
                    $list['data'][$k]['today_amount'] = $v1['today_amount'];
                }
            }
            foreach($gid_all as $k1=>$v1){
                if($v['id'] == $v1['gid']){
                    $list['data'][$k]['qty'] = $v1['qty'];
                    $list['data'][$k]['amount'] = $v1['amount'];
                }
            }
        }
        if($get['key'] && $get['sort']){
            $key  = $get['key'];
            $sort = $get['sort'] == 'asc' ? SORT_ASC : SORT_DESC;
            $column = array_column($list['data'],$key);     // 获取列数据
            array_multisort($column,$sort,$list['data']);  // 多维数组的排序
        }
        return view('countGoods',['list'=>$list['data'],'page'=>$listObj->render()]);
    }
}